<?php

namespace common\component;
use Yii;
use linslin\yii2\curl\Curl;

class Mobile 
{
	/**
	* @desc  错误码
	* @var  HttpRequest
	* @access  public
	*/
	public static  function error_code($retult)
	{
		$config = array(
			 0	=> '发送成功',
			-1	=> '参数格式错误',
			-2	=> '用注册号或者密码错误',
			-3	=> '密码连续三次错误',
			-4	=> '管理员名称或密码错误',
			-5	=> '未知错误',
			-6	=> '注册号不能为空',
			-7	=> '密码不能为空',
			-8	=> '手机号不能为空',
			-9	=> '短信内容不能为空',
			-10	=> '短信内容超长，大于560个字符',
			-11	=> '余额不足',	
	 		-12	=> '短信内容中有非法词',
			-13	=> '短信包中的手机号码数量超过1000条',
			-14	=> '定时时间不能为空',
			-15	=> '非本平台的注册号 ',
			-16 => '注册号不可用',	
			-17 => '充值金额必须为数字',
			-18 => '扣费金额必须为数字',
			-19 => '线程过多，大于4',
			500 => '程序异常',
		);
		return $config[$retult];
	}
	
	/**
	 * @desc 闪信通短信平台 错误码
	 * @var  HttpRequest
	 * @access  public
	 */
	public static  function shx_error_code($retult)
	{
		$config = array(
				0	=> '发送成功',
				-1	=> '用户或者密码错误',
				-2	=> '用户账号被冻结',
				-3	=> '超过最大短信内容长度',
				-4	=> '超过允许发送号码数量',
				-5	=> '内容非法',
				-6	=> '手机号码无效',
				-7	=> '认证失败，修改密码无效',
				-8	=> '账号余额不足',
				-9	=> '服务端异常',
				-10	=> '用户不存在',
				-11	=> '申请试用账号失败',
				-12	=> '定时发送时间错误',
				-13	=> '定时短信时间不能超过3个月',
				-14	=> '定时短信时MsgIdentify不能为空',
				-15	=> '该定时短信不能被取消 ',
		);
		return $config[$retult];
	}

	/**
	 * @desc 发送手机短信
	 * @access public
	 * @param string $phone 手机号码 多个逗号隔开， 最多50个内
	 * @param string $body  短信内容
	 * @return array('status'=>'状态码' , 'message'=>'信息提示' )状态 0为正常，小于0 为错误
	 */
	public static function SendMobileMsg($phone, $body)
    {
    	
    	if (empty(Yii::$app->params['setting']['sms_user']))
    	{
    		return array('status' => -6, 'msg' => self::error_code(-6));
    	}
    	
        $url = 'http://www.stongnet.com/sdkhttp/sendsms.aspx';
        $msg = rawurlencode($body);
        
        $strSmsParam = "reg=".Yii::$app->params['setting']['sms_user']."&pwd=".Yii::$app->params['setting']['sms_pawd']."&sourceadd=&phone={$phone}&content=" . $msg;

		//Init curl
		$curl = new Curl;
		$result = $curl->setOption(CURLOPT_POSTFIELDS,$strSmsParam)->post($url);

        
        if (empty($result))
        {
            return array('status' => -1, 'msg' => '短信发送失败');
        }
        
        parse_str($result, $explode);

        return array('status' => $explode['result'], 'message' => $explode['message']);
    }
    
    /**
     * @desc 闪信通短信平台  发送手机短信
     * @access public
     * @param string $phone 手机号码 多个逗号隔开， 最多50个内
     * @param string $body  短信内容
     * @return array('status'=>'状态码' , 'message'=>'信息提示' )状态 0为正常，小于0 为错误
     */
    public static function SendShxtongMsg($phone, $body)
    {
    	 
    	/* if (empty(Yii::app()->params['setting']['sms_user']))
    	{
    		return array('status' => -6, 'msg' => self::shx_error_code(-6));
    	} */
    	 
    	$url = 'http://119.145.253.67:8080/edeeserver/sendSMS.do';
    	//$msg = mb_convert_encoding($body, 'gb2312','utf-8');
    	$msg = rawurlencode(mb_convert_encoding($body, 'gb2312','utf-8'));
    	$strSmsParam = "UserName=900762&Password=778899&TimeStamp=&MsgIdentify=&MobileNumber={$phone}&MsgContent=" . $msg;
    	//mb_convert_encoding($strSmsParam, 'gb2312', 'utf-8');
    	$result = Yii::$app->curl->post($url,$strSmsParam);
    	//var_dump($result);exit;
    	if ($result != 0)
    	{
    		return array('status' => -1, 'message' => '短信发送失败'.$result);
    	}
    
    	//parse_str($result, $explode);
    
    	return array('status' => $result, 'message' => '短信发送成功');
    }
}
?>